#include "string_utils.h"

char                *global_tempbuff=NULL;

void
mystrncpy(char *d,char *s) {
    if(!s||!strlen(s)) {
        int l=strlen(s);
        if(!d)
            d=(char*)mymalloc(strlen(EMPTY)); 					
        mystrncpy(d,EMPTY); 
    } else {
        int l=strlen(s);
        if(!d)
            d=(char*)mymalloc(l); 	
        mystrncpy(d,s);
    }
}

void 
dump_sc(string_cell_t in) {
	printf("\n* DUMP_SC: *\n");
	printf("*name_sc: 	%s\n",in->name_sc );
	printf("*val_sc: 	%s\n",in->val_sc );
	printf("len_sc: 	%d\n",in->len_sc );
	printf("pos_sc: 	%d\n",in->pos_sc );
	printf("*next_sc: 	%p\n",in->next_sc );
	printf("* DUMP_SC: *\n\n");
}

void 
dump_sm(string_main_t in) {
	printf("\n* DUMP_SM: *\n");
	printf("*name_sm: 	%s\n",in->name_sm );
	printf("*val_sm: 	%s\n",in->val_sm );
	printf("len_sm: 	%d\n",in->len_sm );
	printf("len_cells: 	%d\n",in->len_cells);
	printf("*current_sc:%p\n",in->current_sc );
	printf("*first_sc: 	%p\n",in->first_sc );
	printf("*last_sc: 	%p\n",in->last_sc );
	printf("*next_sm: 	%p\n",in->next_sm );
	printf("* DUMP_SM: *\n\n");
}

void 
init_struct_string() {
	global_strings=(string_global_t)mycalloc(sizeof(struct string_global),1);			
	global_current_sm=(string_main_t)mycalloc(sizeof(struct string_main),1);
	mystrncpy(empty_string,EMPTY);
}

void
print_list(string_main_t a) {
	string_cell_t q=a->first_sc;
	int i;
	for(i=0;q->next_sc!=NULL;i++) {
		printf("%d: %d: \"%s\"=\"%s\"\n",i+1,q->pos_sc,q->name_sc,q->val_sc);
		q=q->next_sc;
	}
}

void *
mycalloc(unsigned int len,unsigned int n) {
    if(len&&n) {
        ATTENTION(len);
        ATTENTION(n);
        void *ret=calloc(len,n);
        if(ret)
            return ret;
        else
            MYPRINT_STR_RET("len*n not allocated\n",NULL);
    } else {
        MYPRINT_STR_RET("lenght||number problem !!\n",NULL);
    }
}

void *
mymalloc(unsigned int len) {
    if(len) {
        ATTENTION(len);
        void *ret=malloc(len);
        if(ret) {
            memset(ret,0,len);
            return ret;
        } else
            MYPRINT_STR_RET("len not allocated\n",NULL);
    } else {
        MYPRINT_STR_RET("lenght problem !!\n",NULL);
    }
}

void global_tbuff(char *str){
    
}
string_cell_t
add_string_cell(string_main_t sm,char *name,char *val,unsigned int len,unsigned int pos) {
    if(sm) {
        string_cell_t sc=(string_cell_t)calloc(sizeof(struct string_cell),1);
        if(sc) {
                sc->name_sc=calloc(strlen(name),1);
                strncpy(sc->name_sc,name,strlen(name));
                sc->val_sc=calloc(strlen(val),1);
                strncpy(sc->val_sc,val,strlen(val));
                sc->len_sc=len;
                sc->pos_sc=pos;
                sc->next_sc=sm->first_sc;
                sm->first_sc=sc;
                sm->len_cells++;
                return sc;
        } else {
                MYPRINT_STR("string_cell not allocated\n");
                return NULL;
        }
    } else {
        MYPRINT_STR("string_main_t==NULL\n");
    }
}

void
delete_string_cell(char *string_main, string_cell_t c) {
	if(string_main) {
		string_main_t p=global_current_sm;
		int found=0;
		while(p->next_sm) {
			if(!strncmp(p->name_sm,string_main,strlen(p->name_sm)))	{
				found=1;
				break;
			}
			p=p->next_sm;
		}
		if(found) {
			if(c==p->first_sc) {
				p->first_sc=c->next_sc;
				free(c);
			} else 
			if(c->next_sc==p->guard) {
				free(p->guard);
				memset(c,'\0',sizeof(struct string_cell));
				p->guard=c;
			} else {
				string_cell_t tmp=c->next_sc;
				//memset(c,'\0',sizeof(struct string_cell)); 
				memcpy(c,c->next_sc,sizeof(struct string_cell));
				free(tmp->name_sc);
				free(tmp->val_sc);
				free(tmp);
			}
		}
	}
}
